var toolbarItemIds = ['toolbar_item_new', 'toolbar_item_folder'];
var toolbarPopupMenus = [[
	['image.gif', 'location', '新建文本文档'],
	['image.gif', 'location', '文件夹'],
	['image.gif', 'location', '其他文档']
], [
	['image.gif', 'location', '1'],
	['image.gif', 'location', '2'],
	['image.gif', 'location', '3']
]];

var toolbarPopupItem = null;
var toolbarPopupItemOver = false;
var toolbarPopupShadow = null;

/**
 * 工具栏弹出菜单初始化
 **/
function initListToolbarPopupMenu() {

	// 添加阴影
	var theBody = document.getElementsByTagName("body")[0];
	var shadowDiv = document.createElement("div");
	shadowDiv.setAttribute("id", "popup_shadow");
	Element.setStyle(shadowDiv, {display:"none", position:"absolute", top:"0", right:"0", width:"0", height:"0", zIndex:"1", backgroundColor:"#ccc"});
	theBody.appendChild(shadowDiv);
	
	// 工具栏项
	for(var i = 0; i < toolbarItemIds.length; i++) {
		buildToolbarPopup(toolbarPopupMenus[i], toolbarItemIds[i] + "_container", toolbarItemIds[i] + "_table"); 
		setToolbarItemAction(toolbarItemIds[i]);
	}
	
	// 点击其他地方的时候隐藏弹出菜单
	document.onclick = popupOffHandle;
}

/**
 * 点击其他地方的时候隐藏弹出菜单
 **/
function popupOffHandle() {
	if(!toolbarPopupItemOver) {
		popupOff();
	}
}

/**
 * 工具栏里面的项的动作
 **/
function setToolbarItemAction(toolbarItemId) {
	var toolbarItem = $(toolbarItemId);
	Event.observe(toolbarItem, "mouseover", overToolbarItem.bindAsEventListener(this, toolbarItem), false);
	Event.observe(toolbarItem, "mouseout", outToolbarItemHandle.bindAsEventListener(this, toolbarItem), false);
	Event.observe(toolbarItem, "click", popupOn.bindAsEventListener(this, toolbarItem), false);
}

/**
 * 工具栏里面的项的动作
 * 鼠标移出 handle
 **/
outToolbarItemHandle = function(event, toolbarItem) {
	outToolbarItem(toolbarItem);
	toolbarPopupItemOver = false;
}

/**
 * 工具栏里面的项的动作
 * 鼠标移出
 **/
outToolbarItem = function(toolbarItem) {
	if(toolbarItem != toolbarPopupItem) {
		toolbarItem.style.backgroundColor = "";
	}
}

/**
 * 工具栏里面的项的动作
 * 鼠标移入
 **/
overToolbarItem = function(event, toolbarItem) {
	if(toolbarItem != toolbarPopupItem) {
		$(toolbarItem.getAttribute("id") + "_bg").style.backgroundColor = "#666";
	}
	toolbarPopupItemOver = true;
}

/**
 * 隐藏弹出菜单
 **/
function popupOff() {
	if(toolbarPopupItem != null) {
		$(toolbarPopupItem.getAttribute("id") + "_container").style.display = "none";
		toolbarPopupItem.style.backgroundColor = ""
		$(toolbarPopupItem.getAttribute("id") + "_bg").style.backgroundColor = "";
		toolbarPopupItem = null;
		$("popup_shadow").style.display = "none";
	}
}

/**
 * 弹出菜单
 **/
function popupOn(event, item) {
	if(toolbarPopupItem == item) {
		return;
	}
	
	popupOff();
	
	// 样式
	$(item.getAttribute("id") + "_bg").style.backgroundColor = "#454";
	//item.style.backgroundColor = "#fff";
	
	// 设置弹出菜单位置
	// --- begin
	var divObj = $(item.getAttribute("id") + "_container");
	var pos = findPos(item);

	divObj.style.left = pos[0];
	divObj.style.top = pos[1] + item.offsetHeight + "px";
	// --- end
	
	// 弹出的菜单放到全局变量中
	toolbarPopupItem = item;
	
	// 显示
	$(toolbarPopupItem.getAttribute("id") + "_container").style.display = "";
	
	// 阴影
	$("popup_shadow").style.width = $(toolbarPopupItem.getAttribute("id") + "_table").offsetWidth + "px";
	$("popup_shadow").style.height = $(toolbarPopupItem.getAttribute("id") + "_table").offsetHeight + "px";
	$("popup_shadow").style.left = pos[0] + 2 + "px";
	$("popup_shadow").style.top = pos[1] + item.offsetHeight + 2 + "px";
	$("popup_shadow").style.display = "";
}

/**
 * 创建工具栏的弹出菜单
 *
 **/
function buildToolbarPopup(arrMenu, popupId, popupTableId) {
	var tbObj = document.createElement("table");
	tbObj.setAttribute("id", popupTableId);
	tbObj.setAttribute("border", "0");
	tbObj.setAttribute("cellpadding", "0");
	tbObj.setAttribute("cellspacing", "1");
	tbObj.setAttribute("bgcolor", "#FFFFFF");
	tbObj.setAttribute("class", "doc_div_pop_menu_tb");
	
	for(var j = 0; j < arrMenu.length; j++) {
		var trObj = document.createElement("tr");
		var tdObj = document.createElement("td");
		tdObj.setAttribute("class", "doc_div_pop_menu_td");
		Event.observe(tdObj, "mouseover", overPopupItem.bindAsEventListener(this, tdObj), false);
		Event.observe(tdObj, "mouseout", outPopupItem.bindAsEventListener(this, tdObj), false);
		tdObj.innerHTML = arrMenu[j][2];
		trObj.appendChild(tdObj);
		tbObj.appendChild(trObj);
	}
	
	var divObj = document.createElement("div");
	divObj.setAttribute("id", popupId);
	divObj.setAttribute("class", "doc_div_pop_menu");
	divObj.appendChild(tbObj);
	divObj.style.display = "none";
	document.getElementsByTagName("body")[0].appendChild(divObj);
}

/**
 * 鼠标进入弹出菜单项的时候，菜单项样式变为高亮显示
 **/
overPopupItem = function(event, tdItem) {
	tdItem.setAttribute("class", "doc_div_pop_menu_td_over");
}

/**
 * 鼠标离开弹出菜单项的时候，菜单项样式复原
 **/
outPopupItem = function(event, tdItem) {
	tdItem.setAttribute("class", "doc_div_pop_menu_td");
}
